<?php

namespace UC_PHP\FileSystem;

if (!defined('UC_FILESYSTEM_ROOT')) 
{
    define('UC_FILESYSTEM_ROOT', dirname(__FILE__) . '/');
    require_once UC_FILESYSTEM_ROOT . 'Autoloader.php';
}

/*
 * For now only ANSI files support
 */

class TextStream
{

    public function __construct($path, $mode = IO_Mode::FOR_READING, $create = false)
    {
        if(IO_Mode::Defines($mode))
        {
            $this->mode =$mode;
        }
        else
        {
            throw new InvalidArgumentException(
                'Invalid file IO mode.');            
        }
        
        if(is_file($path))
        {
            $this->handle = fopen($path, $this->mode);
            $this->path = $path;
        }
        elseif($create)
        {
            $this->handle = fopen($path, $this->mode);
            $this->path = $path;
        }
        else
        {
            throw new \InvalidArgumentException(
                "The file $path does not exist.");
        }
    }
    
    public function at_end_of_line()
    {
        if($this->mode== IO_Mode::FOR_READING)
        {
            $eol_length = strlen(PHP_EOL);

            if($eol_length == 1)
            {
                if(!feof($this->handle))
                {
                    if(fgetc($this->handle) == PHP_EOL)
                    {
                        fseek($this->handle, -1, SEEK_CUR);
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                    return false;
                }
            }
            elseif($eol_length == 2)
            {
                if($t = fgetc($this->handle))
                {
                    if($t1 = fgetc($this->handle))
                    {
                        fseek($this->handle, -2, SEEK_CUR);
                        if($t . $t1 == PHP_EOL)
                        {
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                    else
                    {
                        fseek($this->handle, -1, SEEK_CUR);
                        return false;
                    }
                }
                else
                {
                    return false;
                }
            }
        }
        else
        {
            throw new InvalidArgumentException(
                'Invalid file IO mode.');
        }
    }

    public function at_end_of_stream()
    {
        return feof($this->handle);
    }
    
    public function close()
    {
        fclose($this->handle);
    }
    
    public function read($characters)
    {
        if($this->mode== IO_Mode::FOR_READING)
        {
            if(is_int($characters) && $characters > 0)
            {
                $string = fread($this->handle, $characters);
                if($string)
                {
                    return $string;
                }
                else
                {
                    throw new FileSystemException(
                        'Error reading file.');
                }
            }
            else
            {
                throw new InvalidArgumentException(
                    'The character number must be a positive integer.');
            }
        }
        else
        {
            throw new InvalidArgumentException(
                'Invalid file IO mode.');
        }
    }
    
    public function read_all()
    {
        if($this->mode == IO_Mode::FOR_READING)
        {
            return file_get_contents($this->path);
        }
        else
        {
            throw new InvalidArgumentException(
                'Invalid file IO mode.');
        }
    }
    
    public function read_line()
    {
        if($this->mode == IO_Mode::FOR_READING)
        {
            return fgets($this->handle);
        }
        else 
        {
            throw new InvalidArgumentException(
                'Invalid file IO mode.');
        }
    }
    
    public function skip($characters)
    {
        if($this->mode == IO_Mode::FOR_READING)
        {
            if(is_int($characters) && $characters > 0)
            {
                if(!fread($this->handle, $characters))
                {
                    return false;
                }
                else
                {
                    return true;
                }
                    
            }
            else
            {
                throw new InvalidArgumentException(
                    'The character number must be a positive integer.');
            }
        }
        else
        {
            throw new InvalidArgumentException(
                'Invalid file IO mode.');
        }
    }
    
    public function skip_line()
    {
        if($this->mode == IO_Mode::FOR_READING)
        {
            fgets($this->handle);
        }
        else
        {
            throw new InvalidArgumentException(
                'Invalid file IO mode.');
        }
    }
    
    public function write($string)
    {
        if($this->mode == IO_Mode::FOR_WRITING 
            || $this->mode == IO_Mode::FOR_APPENDING)
        {
            if(!fwrite($this->handle, $string))
            {
                throw new FileSystemException(
                    'Error writing to a file.');
            }
        }
        else
        {
            throw new InvalidArgumentException(
                'Invalid file IO mode');
        }
    }
    
    public function write_blank_lines($count)
    {
        for($i = 0; $i < $count; $i++)
        {
            $this->write(PHP_EOL);
        }
    }
    
    public function write_line($string)
    {
        $this->write($string . PHP_EOL);
    }
    
    private $mode;
    private $path;
    private $handle;
}
